<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Vehicle extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'vehicle';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'is_recommended_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getIsRecommendedList()
    {
        return ['1' => __('Is_recommended 1'), '2' => __('Is_recommended 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRecommendedTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recommended']) ? $data['is_recommended'] : '');
        $list = $this->getIsRecommendedList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreateTimeAttr($value, $data)
    {
        return date('Y-m-d H:i:s',$value);
    }
    public function getUpdateTimeAttr($value, $data)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getThumbnailImageAttr($value, $data)
    {

        $value = $value ? $value : (isset($data['thumbnail_image'])&&$data['thumbnail_image'] ?$data['thumbnail_image']  : 'avatar.png');
        return get_app_image_url($value);
    }


    public function category()
    {
        return $this->belongsTo('Category', 'category_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
